<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Vue,Vue-cli," />





  <link rel="alternate" href="/atom.xml" title="OBKoro1's Blog" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="前言在vue-cli 2.X的时候，也写过一篇类似的文章，在八月份的时候vue-cli已经更新到了3.X，新版本的脚手架，功能灰常强大，试用过后非常喜欢，写篇教程来帮助各位踩一下坑。">
<meta name="keywords" content="Vue,Vue-cli">
<meta property="og:type" content="article">
<meta property="og:title" content="一份超级详细的Vue-cli3.0使用教程">
<meta property="og:url" content="http://yoursite.com/2018/11/11/一份超级详细的Vue-cli3-0使用教程/index.html">
<meta property="og:site_name" content="OBKoro1&#39;s Blog">
<meta property="og:description" content="前言在vue-cli 2.X的时候，也写过一篇类似的文章，在八月份的时候vue-cli已经更新到了3.X，新版本的脚手架，功能灰常强大，试用过后非常喜欢，写篇教程来帮助各位踩一下坑。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/OBKoro1/articleImg_src/blob/master/weibo_img_move/005Y4rCogy1fx36ohniokj30xc0fcaek.jpg?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fc87173c427ea?w=1672&h=1754&f=png&s=344139?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fca9d5b691cc3?w=507&h=82&f=png&s=10124?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fcd735ce563ba?w=214&h=520&f=png&s=33855?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fcd823cce157b?w=3352&h=1938&f=png&s=259698?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd034aae0942d?w=1918&h=999&f=png&s=86025?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd0f5359fc7ba?w=980&h=389&f=png&s=147646?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd177edf662d3?w=651&h=591&f=png&s=61598?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd1a86f0e7bd5?w=1029&h=558&f=png&s=83704?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd276f5a4de8b?w=1918&h=999&f=png&s=214672?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd344e9e5edc0?w=1916&h=998&f=png&s=150720?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd3595b37e06a?w=1535&h=972&f=png&s=223096?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd391835d2edb?w=1915&h=996&f=png&s=287780?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd3c81be26fd0?w=1911&h=990&f=png&s=248696?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd42dae770e0d?w=1731&h=906&f=png&s=121200?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd4f37d2fd567?w=1913&h=994&f=png&s=145551?raw=true">
<meta property="og:updated_time" content="2019-07-20T06:55:39.061Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="一份超级详细的Vue-cli3.0使用教程">
<meta name="twitter:description" content="前言在vue-cli 2.X的时候，也写过一篇类似的文章，在八月份的时候vue-cli已经更新到了3.X，新版本的脚手架，功能灰常强大，试用过后非常喜欢，写篇教程来帮助各位踩一下坑。">
<meta name="twitter:image" content="https://github.com/OBKoro1/articleImg_src/blob/master/weibo_img_move/005Y4rCogy1fx36ohniokj30xc0fcaek.jpg?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/11/11/一份超级详细的Vue-cli3-0使用教程/"/>





  <title>一份超级详细的Vue-cli3.0使用教程 | OBKoro1's Blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?64d57d0b926bbd27431daa89d2e0a76b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">OBKoro1's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-前端算法">
          <a href="/arithmetic/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br />
            
            前端算法
          </a>
        </li>
      
        
        <li class="menu-item menu-item-博客最新更新">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            博客最新更新
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/11/11/一份超级详细的Vue-cli3-0使用教程/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="OBKoro1">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/hand1.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="OBKoro1's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">一份超级详细的Vue-cli3.0使用教程</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-11-11T13:57:57+08:00">
                2018-11-11
              </time>
            

            

            
          </span>

          

          
            
          

          
          
             <span id="/2018/11/11/一份超级详细的Vue-cli3-0使用教程/" class="leancloud_visitors" data-flag-title="一份超级详细的Vue-cli3.0使用教程">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,162 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  8分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><img src="https://github.com/OBKoro1/articleImg_src/blob/master/weibo_img_move/005Y4rCogy1fx36ohniokj30xc0fcaek.jpg?raw=true" alt=""></p>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>在vue-cli 2.X的时候，也写过一篇类似的<a href="https://juejin.im/post/597eee92f265da3e2e56e37c" target="_blank" rel="noopener">文章</a>，在八月份的时候vue-cli已经更新到了3.X，新版本的脚手架，功能灰常强大，试用过后非常喜欢，写篇教程来帮助各位踩一下坑。</p>
<a id="more"></a>
<blockquote>
<p>游泳、健身了解一下：<a href="http://obkoro1.com/" target="_blank" rel="noopener">博客</a>、<a href="http://obkoro1.com/web_accumulate/accumulate/" target="_blank" rel="noopener">前端积累文档</a>、<a href="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1631b6f52f7e7015.jpeg?raw=true" target="_blank" rel="noopener">公众号</a>、<a href="https://github.com/OBKoro1" target="_blank" rel="noopener">GitHub</a></p>
</blockquote>
<h3 id="主要内容："><a href="#主要内容：" class="headerlink" title="主要内容："></a>主要内容：</h3><ol>
<li>零配置启动/打包一个<code>.vue</code>文件</li>
<li>详细的搭建过程</li>
<li><strong>重点推荐：使用图形化界面创建/管理/运行项目</strong></li>
</ol>
<hr>
<h2 id="安装："><a href="#安装：" class="headerlink" title="安装："></a>安装：</h2><h3 id="卸载旧版本："><a href="#卸载旧版本：" class="headerlink" title="卸载旧版本："></a>卸载旧版本：</h3><p>如果你事先已经全局安装了旧版本的<code>vue-cli</code>(1.x 或 2.x),你需要先卸载它:</p>
<pre><code>npm uninstall vue-cli -g
</code></pre><h3 id="Node版本要求："><a href="#Node版本要求：" class="headerlink" title="Node版本要求："></a>Node版本要求：</h3><p>3.x需要在<code>Node.js</code>8.9或更高版本(推荐8.11.0+)，点击这里可以安装<a href="http://nodejs.cn/download/" target="_blank" rel="noopener">node</a></p>
<p>大多数人都安装过了node,使用下面的命令行<strong>查询你的node版本</strong>:</p>
<pre><code>node -v
</code></pre><p>如果你的版本不够，可以使用下面的命令行来把<strong>Node版本更新到最新的稳定版</strong>：</p>
<pre><code>npm install -g n // 安装模块 这个模块是专门用来管理node.js版本的
n stable // 更新你的node版本
</code></pre><p>mac下，更新版本的时候,如果提示你权限不够：</p>
<pre><code>sudo n stable // 我就遇到了
</code></pre><h3 id="安装vue-cli"><a href="#安装vue-cli" class="headerlink" title="安装vue-cli:"></a>安装vue-cli:</h3><pre><code>npm install -g @vue/cli // 安装cli3.x
vue --version // 查询版本是否为3.x
</code></pre><p>如果cli3.x用的不舒服，<strong>cli3也能使用2.x模板</strong>：</p>
<pre><code>npm install -g @vue/cli-init // 安装这个模块
// 就可以使用2.x的模板：vue init webpack my-project
</code></pre><h2 id="零配置启动-打包一个-vue文件："><a href="#零配置启动-打包一个-vue文件：" class="headerlink" title="零配置启动/打包一个.vue文件："></a>零配置启动/打包一个<code>.vue</code>文件：</h2><h3 id="安装扩展："><a href="#安装扩展：" class="headerlink" title="安装扩展："></a>安装扩展：</h3><pre><code>npm install -g @vue/cli-service-global
</code></pre><p>安装完扩展之后，可以随便找个文件夹建一个如下方示例的.vue文件,然后跑起来：</p>
<pre><code>vue serve App.vue // 启动服务
vue build App.vue // 打包出生产环境的包并用来部署
</code></pre><h3 id="如下图，只需一个-vue文件，就能迅速启动一个服务："><a href="#如下图，只需一个-vue文件，就能迅速启动一个服务：" class="headerlink" title="如下图，只需一个.vue文件，就能迅速启动一个服务："></a>如下图，只需一个.vue文件，就能迅速启动一个服务：</h3><p>如图所示，服务启动的时候回生成一个<code>node_modules</code>包，稍微测试了一下，<strong>服务支持ES6语法和热更新</strong>，打包的时候会生成一个<code>dist</code>文件夹。(新建一个test.vue文件也只有一个<code>node_modules</code>/<code>dist</code>文件夹)</p>
<p>这是个很棒的功能，用于<strong>开发一个库、组件，做一些小demo等都是非常适合的</strong>！</p>
<p><img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fc87173c427ea?w=1672&amp;h=1754&amp;f=png&amp;s=344139?raw=true" alt=""></p>
<hr>
<h2 id="第一次创建项目"><a href="#第一次创建项目" class="headerlink" title="第一次创建项目:"></a>第一次创建项目:</h2><h3 id="1-命令行"><a href="#1-命令行" class="headerlink" title="1. 命令行:"></a>1. 命令行:</h3><pre><code>vue create hello-cli3 
</code></pre><ul>
<li><p>hello-cli3是文件夹名字，如果不存在会<strong>自动创建文件夹</strong>，如果存在会安装到那个文件夹中。</p>
</li>
<li><p>相比2.x的时候需要自己手动创建一个文件夹，这里也算是一个小优化吧。</p>
</li>
</ul>
<h3 id="2-选择模板"><a href="#2-选择模板" class="headerlink" title="2. 选择模板:"></a>2. 选择模板:</h3><ul>
<li><p>一开始只有两个选项: <code>default</code>(默认配置)和<code>Manually select features</code>(手动配置)</p>
<p>  默认配置只有<code>babel</code>和<code>eslint</code>其他的都要自己另外再配置，所以我们选第二项手动配置。</p>
</li>
<li><p>在每次选择手动配置之后，会询问你是否保存配置，也就是图片中的<code>koro</code>选项，这样以后我们在进行创建项目的时候<strong>只需使用原先的配置</strong>就可以了，而不用再进行配置。</p>
</li>
</ul>
<p><img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fca9d5b691cc3?w=507&amp;h=82&amp;f=png&amp;s=10124?raw=true" alt=""></p>
<h3 id="3-选择配置："><a href="#3-选择配置：" class="headerlink" title="3. 选择配置："></a>3. 选择配置：</h3><ul>
<li><p>根据你的项目需要来选择配置,空格键是选中与取消，A键是全选</p>
<pre><code>? Check the features needed for your project: (Press &lt;space&gt; to select, &lt;a&gt; to toggle all, &lt;i&gt; to invert selection) 
// 检查项目所需的功能:(按&lt;space&gt;选择，&lt;a&gt;切换所有，&lt;i&gt;反转选择）
&gt;( ) TypeScript                                 // 支持使用 TypeScript 书写源码
 ( ) Progressive Web App (PWA) Support          // PWA 支持
 ( ) Router                                     // 支持 vue-router
 ( ) Vuex                                       // 支持 vuex
 ( ) CSS Pre-processors                         // 支持 CSS 预处理器。
 ( ) Linter / Formatter                         // 支持代码风格检查和格式化。
 ( ) Unit Testing                               // 支持单元测试。
 ( ) E2E Testing  
</code></pre></li>
</ul>
<h3 id="4-选择css预处理器"><a href="#4-选择css预处理器" class="headerlink" title="4. 选择css预处理器:"></a>4. 选择css预处理器:</h3><ul>
<li><p>如果你选择了Css预处理器选项，会让你选择这个</p>
<pre><code>? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default):
// 选择CSS预处理器（默认支持PostCSS，Autoprefixer和CSS模块）：
&gt; SCSS/SASS
  LESS
  Stylus
</code></pre></li>
</ul>
<h3 id="5-是否使用路由的history模式："><a href="#5-是否使用路由的history模式：" class="headerlink" title="5. 是否使用路由的history模式："></a>5. 是否使用路由的<code>history</code>模式：</h3><ul>
<li>这里我建议选No，这样打包出来丢到服务器上可以直接使用了，后期要用的话，也可以自己再开起来。</li>
<li><p>选yes的话需要服务器那边再进行设置。</p>
<pre><code>Use history mode for router? (Requires proper server setup for index fallback in production) 
// 路由使用history模式?(在生产环境中需要适当的服务器设置以备索引)
</code></pre></li>
</ul>
<h3 id="6-选择Eslint代码验证规则："><a href="#6-选择Eslint代码验证规则：" class="headerlink" title="6. 选择Eslint代码验证规则："></a>6. 选择Eslint代码验证规则：</h3><pre><code>&gt; ESLint with error prevention only
  ESLint + Airbnb config
  ESLint + Standard config
  ESLint + Prettier
</code></pre><h3 id="7-选择什么时候进行代码规则检测："><a href="#7-选择什么时候进行代码规则检测：" class="headerlink" title="7. 选择什么时候进行代码规则检测："></a>7. 选择什么时候进行代码规则检测：</h3><ul>
<li>建议选保存就检测，等到commit的时候，问题可能都已经积累很多了。</li>
<li><p>之前写了篇<a href="http://obkoro1.com/web_accumulate/accumulate/tool/Eslint%E8%87%AA%E5%8A%A8%E4%BF%AE%E5%A4%8D%E6%A0%BC%E5%BC%8F%E9%94%99%E8%AF%AF.html#vscode%E4%BF%9D%E5%AD%98%E6%97%B6%E8%87%AA%E5%8A%A8%E4%BF%AE%E5%A4%8Deslint%E9%94%99%E8%AF%AF" target="_blank" rel="noopener">VsCode保存时自动修复Eslint错误</a>推荐一下。</p>
<pre><code>? Pick additional lint features: (Press &lt;space&gt; to select, &lt;a&gt; to toggle all, &lt;i&gt; to invert selection)
&gt;( ) Lint on save // 保存就检测
 ( ) Lint and fix on commit // fix和commit时候检查
</code></pre></li>
</ul>
<h3 id="8-选择e2e测试"><a href="#8-选择e2e测试" class="headerlink" title="8. 选择e2e测试:"></a>8. 选择e2e测试:</h3><pre><code>? Pick a E2E testing solution: (Use arrow keys)
❯ Cypress (Chrome only) 
  Nightwatch (Selenium-based) 
</code></pre><h3 id="9-把babel-postcss-eslint这些配置文件放哪："><a href="#9-把babel-postcss-eslint这些配置文件放哪：" class="headerlink" title="9. 把babel,postcss,eslint这些配置文件放哪："></a>9. 把babel,postcss,eslint这些配置文件放哪：</h3><ul>
<li><p>通常我们会选择独立放置，让package.json干净些</p>
<pre><code>? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? (Use arrow keys)
&gt; In dedicated config files // 独立文件放置
  In package.json // 放package.json里
</code></pre></li>
</ul>
<h3 id="10-是否保存配置："><a href="#10-是否保存配置：" class="headerlink" title="10. 是否保存配置："></a>10. 是否保存配置：</h3><pre><code>Save this as a preset for future projects? (Y/n) // 是否记录一下以便下次继续使用这套配置
// 选保存之后，会让你写一个配置的名字：
Save preset as:  name // 然后你下次进入配置可以直接使用你这次的配置了
</code></pre><h3 id="11-下载依赖"><a href="#11-下载依赖" class="headerlink" title="11. 下载依赖"></a>11. 下载依赖</h3><h3 id="12-webpack配置的目录不见了："><a href="#12-webpack配置的目录不见了：" class="headerlink" title="12. webpack配置的目录不见了："></a>12. webpack配置的目录不见了：</h3><p>一起来看一下新项目的结构(下图),会发现2.x的webpack配置的目录不见了，也就是没有build、config这两个文件夹了：</p>
<ul>
<li>这种方式的优势<strong>对小白来说非常友好</strong>，不会一上来就两个文件夹，一堆文件，看着脑袋都大了。</li>
<li>然后在<strong>引用<del>抄</del>别人的配置的时候，也非常方便</strong>，直接将文件复制过来就好了。</li>
<li><p><strong>在自定义一下webpack的配置</strong>，我们需要在<strong>根目录新建一个<code>vue.config.js</code>文件</strong>，文件中应该导出一个对象，然后进行配置，详情查阅<a href="https://cli.vuejs.org/zh/config/" target="_blank" rel="noopener">官方文档</a></p>
<pre><code>// vue.config.js
module.exports = {
  // 选项...
}
</code></pre></li>
</ul>
<ul>
<li><p>还有一些小变动像：static文件夹改为public了，router文件夹变成了单个文件之类的(我之前一直这么做,嘿嘿)。</p>
<p>  <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fcd735ce563ba?w=214&amp;h=520&amp;f=png&amp;s=33855?raw=true" alt=""></p>
</li>
</ul>
<h3 id="13-启动项目："><a href="#13-启动项目：" class="headerlink" title="13.启动项目："></a>13.启动项目：</h3><ul>
<li><p>启动项目：npm run serve // <strong>不是之前的 npm run dev</strong></p>
</li>
<li><p>打开<code>http://localhost:8080</code>：</p>
<p> <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fcd823cce157b?w=3352&amp;h=1938&amp;f=png&amp;s=259698?raw=true" alt=""></p>
</li>
</ul>
<hr>
<h2 id="使用图形化界面创建-管理-运行项目："><a href="#使用图形化界面创建-管理-运行项目：" class="headerlink" title="使用图形化界面创建/管理/运行项目："></a>使用图形化界面创建/管理/运行项目：</h2><h3 id="启动图形化界面"><a href="#启动图形化界面" class="headerlink" title="启动图形化界面"></a>启动图形化界面</h3><pre><code>vue ui 
</code></pre><ul>
<li>这是个全局的命令 在哪个文件夹都可以打开</li>
<li><p>界面(下图)，重要的项目可以收藏起来(置顶)：</p>
<p>  <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd034aae0942d?w=1918&amp;h=999&amp;f=png&amp;s=86025?raw=true" alt=""></p>
</li>
</ul>
<h3 id="创建项目和导入项目："><a href="#创建项目和导入项目：" class="headerlink" title="创建项目和导入项目："></a>创建项目和导入项目：</h3><ol>
<li><p>目录选中之后，导入项目点击下面的导入就可以了。</p>
<p> <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd0f5359fc7ba?w=980&amp;h=389&amp;f=png&amp;s=147646?raw=true" alt=""></p>
</li>
<li><p>创建项目，填一个文件夹名字：</p>
<p> <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd177edf662d3?w=651&amp;h=591&amp;f=png&amp;s=61598?raw=true" alt=""></p>
</li>
<li><p>然后选一下预先保存好的设置就可以了，非常方便，建议采用图形界面来创建项目：</p>
<p> <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd1a86f0e7bd5?w=1029&amp;h=558&amp;f=png&amp;s=83704?raw=true" alt=""></p>
</li>
</ol>
<h3 id="项目管理："><a href="#项目管理：" class="headerlink" title="项目管理："></a>项目管理：</h3><p>当我们点击hello -cli3项目，就会进入项目管理的界面</p>
<h4 id="1-仪表盘："><a href="#1-仪表盘：" class="headerlink" title="1. 仪表盘："></a>1. 仪表盘：</h4><ul>
<li>这个仪表盘，主要是为了我们操作方便而设置的</li>
<li><p>可以点击右上角的按钮，来添加/移动这些功能选项。</p>
<p>  <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd276f5a4de8b?w=1918&amp;h=999&amp;f=png&amp;s=214672?raw=true" alt=""></p>
</li>
</ul>
<h4 id="2-vue-cli3-x插件："><a href="#2-vue-cli3-x插件：" class="headerlink" title="2. vue-cli3.x插件："></a>2. vue-cli3.x插件：</h4><ul>
<li><p>vue-cli3的插件功能，详情了解<a href="https://cli.vuejs.org/zh/guide/plugins-and-presets.html#%E6%8F%92%E4%BB%B6" target="_blank" rel="noopener">官方文档</a></p>
<p>  <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd344e9e5edc0?w=1916&amp;h=998&amp;f=png&amp;s=150720?raw=true" alt=""></p>
</li>
<li><p>cli3插件安装的过程：</p>
<p>  <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd3595b37e06a?w=1535&amp;h=972&amp;f=png&amp;s=223096?raw=true" alt="cli3插件安装的过程"></p>
</li>
</ul>
<h4 id="3-项目依赖"><a href="#3-项目依赖" class="headerlink" title="3. 项目依赖"></a>3. 项目依赖</h4><ul>
<li>直接在图形界面管理依赖很舒服了！</li>
<li><p>安装依赖的时候，要记得选择开发依赖/运行依赖！</p>
<p>  <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd391835d2edb?w=1915&amp;h=996&amp;f=png&amp;s=287780?raw=true" alt=""></p>
</li>
</ul>
<h4 id="4-项目配置"><a href="#4-项目配置" class="headerlink" title="4. 项目配置"></a>4. 项目配置</h4><ul>
<li><p>可以对cli进行一些配置、Eslint规则修改：</p>
<p>  <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd3c81be26fd0?w=1911&amp;h=990&amp;f=png&amp;s=248696?raw=true" alt=""></p>
</li>
</ul>
<h4 id="5-任务："><a href="#5-任务：" class="headerlink" title="5. 任务："></a>5. 任务：</h4><ul>
<li>serve 运行项目，点击直接运行，再也不用输入命令了！</li>
<li>可以清楚的看到各个模块用了多久，方便我们<strong>针对性的进行优化</strong>：</li>
</ul>
<pre><code>![](https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd41bde538496?w=1913&amp;h=996&amp;f=png&amp;s=223258?raw=true)
</code></pre><ul>
<li><p>build 打包项目：这里<strong>主要展示了图表的功能</strong>，比以前2.x生成报告更加直观，超级棒！</p>
<p>  <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd42dae770e0d?w=1731&amp;h=906&amp;f=png&amp;s=121200?raw=true" alt=""></p>
</li>
</ul>
<h4 id="6-其他"><a href="#6-其他" class="headerlink" title="6. 其他"></a>6. 其他</h4><ul>
<li>夜间风格界面，我更喜欢这个界面</li>
<li>直接打开编辑器,很棒了！</li>
<li><p>还有一些乱七八糟的按钮</p>
<p>  <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/166fd4f37d2fd567?w=1913&amp;h=994&amp;f=png&amp;s=145551?raw=true" alt=""></p>
</li>
</ul>
<hr>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><p>可以说很认真了，希望大家看完能够有些收获，<strong>赶紧试试新版的vue-cli吧</strong>！</p>
<h3 id="希望看完的朋友可以点个喜欢-关注，您的支持是对我最大的鼓励。"><a href="#希望看完的朋友可以点个喜欢-关注，您的支持是对我最大的鼓励。" class="headerlink" title="希望看完的朋友可以点个喜欢/关注，您的支持是对我最大的鼓励。"></a>希望看完的朋友可以点个喜欢/关注，您的支持是对我最大的鼓励。</h3><p><a href="http://obkoro1.com/" target="_blank" rel="noopener">博客</a>、<a href="http://obkoro1.com/web_accumulate/accumulate/" target="_blank" rel="noopener">前端积累文档</a>、<a href="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1631b6f52f7e7015.jpeg?raw=true" target="_blank" rel="noopener">公众号</a>、<a href="https://github.com/OBKoro1" target="_blank" rel="noopener">GitHub</a></p>
<p> 以上2018.11.10</p>
<p> 参考资料：</p>
<p> <a href="https://cli.vuejs.org/zh/guide/" target="_blank" rel="noopener">vue-cli3官方文档</a></p>
<p> <a href="https://gitee.com/hjm100/codes/rjch7b31l4f59gt8suidn63" target="_blank" rel="noopener">vue-cli3.0搭建与配置</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>听说，打赏我的人最后都找到了真爱。</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/pay/weixin.jpg" alt="OBKoro1 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/pay/ali.jpg" alt="OBKoro1 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Vue/" rel="tag"><i class="fa fa-tag"></i> Vue</a>
          
            <a href="/tags/Vue-cli/" rel="tag"><i class="fa fa-tag"></i> Vue-cli</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/11/04/算法-奇偶分割数组/" rel="next" title="算法-奇偶分割数组">
                <i class="fa fa-chevron-left"></i> 算法-奇偶分割数组
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/11/28/前端er来学习一下webWorker吧/" rel="prev" title="前端er来学习一下webWorker吧">
                前端er来学习一下webWorker吧 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          
            <img class="site-author-image" itemprop="image"
              src="/uploads/hand1.jpg"
              alt="OBKoro1" />
          
            <p class="site-author-name" itemprop="name">OBKoro1</p>
            <p class="site-description motion-element" itemprop="description">种一棵树最好的时间是十年前,其次是现在</p>
        </div>

        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
            
              <a href="/archives">
            
                <span class="site-state-item-count">105</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">20</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">74</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/OBKoro1" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>GitHub</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://juejin.im/user/58714f0eb123db4a2eb95372" target="_blank" title="掘金">
                  
                    <i class="fa fa-fw fa-globe"></i>掘金</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://obkoro1.com/web_accumulate/" target="_blank" title="前端积累">
                  
                    <i class="fa fa-fw fa-globe"></i>前端积累</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://segmentfault.com/u/obkoro1/articles" target="_blank" title="segmentfault">
                  
                    <i class="fa fa-fw fa-globe"></i>segmentfault</a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#主要内容："><span class="nav-number">1.1.</span> <span class="nav-text">主要内容：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装："><span class="nav-number">2.</span> <span class="nav-text">安装：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#卸载旧版本："><span class="nav-number">2.1.</span> <span class="nav-text">卸载旧版本：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Node版本要求："><span class="nav-number">2.2.</span> <span class="nav-text">Node版本要求：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#安装vue-cli"><span class="nav-number">2.3.</span> <span class="nav-text">安装vue-cli:</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#零配置启动-打包一个-vue文件："><span class="nav-number">3.</span> <span class="nav-text">零配置启动/打包一个.vue文件：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装扩展："><span class="nav-number">3.1.</span> <span class="nav-text">安装扩展：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如下图，只需一个-vue文件，就能迅速启动一个服务："><span class="nav-number">3.2.</span> <span class="nav-text">如下图，只需一个.vue文件，就能迅速启动一个服务：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#第一次创建项目"><span class="nav-number">4.</span> <span class="nav-text">第一次创建项目:</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-命令行"><span class="nav-number">4.1.</span> <span class="nav-text">1. 命令行:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-选择模板"><span class="nav-number">4.2.</span> <span class="nav-text">2. 选择模板:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-选择配置："><span class="nav-number">4.3.</span> <span class="nav-text">3. 选择配置：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-选择css预处理器"><span class="nav-number">4.4.</span> <span class="nav-text">4. 选择css预处理器:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-是否使用路由的history模式："><span class="nav-number">4.5.</span> <span class="nav-text">5. 是否使用路由的history模式：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-选择Eslint代码验证规则："><span class="nav-number">4.6.</span> <span class="nav-text">6. 选择Eslint代码验证规则：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-选择什么时候进行代码规则检测："><span class="nav-number">4.7.</span> <span class="nav-text">7. 选择什么时候进行代码规则检测：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-选择e2e测试"><span class="nav-number">4.8.</span> <span class="nav-text">8. 选择e2e测试:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-把babel-postcss-eslint这些配置文件放哪："><span class="nav-number">4.9.</span> <span class="nav-text">9. 把babel,postcss,eslint这些配置文件放哪：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#10-是否保存配置："><span class="nav-number">4.10.</span> <span class="nav-text">10. 是否保存配置：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#11-下载依赖"><span class="nav-number">4.11.</span> <span class="nav-text">11. 下载依赖</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#12-webpack配置的目录不见了："><span class="nav-number">4.12.</span> <span class="nav-text">12. webpack配置的目录不见了：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#13-启动项目："><span class="nav-number">4.13.</span> <span class="nav-text">13.启动项目：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用图形化界面创建-管理-运行项目："><span class="nav-number">5.</span> <span class="nav-text">使用图形化界面创建/管理/运行项目：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#启动图形化界面"><span class="nav-number">5.1.</span> <span class="nav-text">启动图形化界面</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建项目和导入项目："><span class="nav-number">5.2.</span> <span class="nav-text">创建项目和导入项目：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#项目管理："><span class="nav-number">5.3.</span> <span class="nav-text">项目管理：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-仪表盘："><span class="nav-number">5.3.1.</span> <span class="nav-text">1. 仪表盘：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-vue-cli3-x插件："><span class="nav-number">5.3.2.</span> <span class="nav-text">2. vue-cli3.x插件：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-项目依赖"><span class="nav-number">5.3.3.</span> <span class="nav-text">3. 项目依赖</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-项目配置"><span class="nav-number">5.3.4.</span> <span class="nav-text">4. 项目配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-任务："><span class="nav-number">5.3.5.</span> <span class="nav-text">5. 任务：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-其他"><span class="nav-number">5.3.6.</span> <span class="nav-text">6. 其他</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结语"><span class="nav-number">6.</span> <span class="nav-text">结语</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#希望看完的朋友可以点个喜欢-关注，您的支持是对我最大的鼓励。"><span class="nav-number">6.1.</span> <span class="nav-text">希望看完的朋友可以点个喜欢/关注，您的支持是对我最大的鼓励。</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2017 &mdash; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">OBKoro1</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">
      156.0k
    </span>
  
</div>




<div>
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span class="site-uv"><i class="fa fa-user"></i> 访问用户： <span class="busuanzi-value" id="busuanzi_value_site_uv">21621</span> 人 </span>
<span class="site-pv"><i class="fa fa-eye"></i> 访问次数： <span class="busuanzi-value" id="busuanzi_value_site_pv">68503</span> 次</span>

</div>



        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.2"></script>



  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>


  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  








  



  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("9ANFp60h6oVyxIPGlau3NOA4-gzGzoHsz", "tMdNuKMbFtN4QsekeNVu83TV");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

  

  

</body>
</html>

